﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using BCCommon;
using BCDto.MakeStaticPagingComponent.TemplateTag;
using BCService.MakeStaticPagingComponent.TemplateTagService;

namespace BCTMSRESTful.Controllers.Management.MakeStaticPagingComponent
{
    /// <summary>
    /// 模板标记控制器
    /// </summary>
    [ApiExplorerSettings(GroupName = "MakeStaticPage")]
    [Route("api/v1/sim/[controller]")]
    public class TemplateTagsController : ManagementBaseController
    {
        private readonly ITemplateTagService templateTagService;

        public TemplateTagsController(ITemplateTagService templateTagService)
        {
            this.templateTagService = templateTagService;
        }
      
        /// <summary>
        /// 获取模板标签名称及说明列表
        /// </summary>
        /// <response code="200">返回模板标记名称及说明列表。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IDictionary<string, string>), 200)]
        public IActionResult GetTagNameAndTagDescription()
        {
            return this.Ok(this.templateTagService.GetTagNameAndTagDescription());
        }

        /// <summary>
        /// 获取模板标签名称列表
        /// </summary>
        /// <response code="200">返回模板标记名称列表。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<string>), 200)]
        public IActionResult GetTagNames()
        {
            return this.Ok(this.templateTagService.GetTemplateTagName());
        }
       
        /// <summary>
        /// 获取模板标签信息列表(全部)
        /// </summary>
        /// <response code="200">返回模板标记信息列表。</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(IEnumerable<TemplateTagDto>), 200)]
        public IActionResult Get()
        {
            return this.Ok(this.templateTagService.Get());
        }
   
        /// <summary>
        /// 获取模板标签信息列表(分页)
        /// </summary>
        /// <param name="tagName">标签名称</param>
        /// <param name="tagDescription">标签描述</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="sortField">排序字段</param>
        /// <param name="ascending">排序</param>
        /// <param name="index">当前页码</param>
        /// <param name="size">每页数据条数</param>
        /// <response code="200">返回模板标记信息列表。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<TemplateTagDto>), 200)]
        public IActionResult Query([FromQuery(Name = "tag_name")]string tagName,
            [FromQuery(Name = "tag_description")]string tagDescription,
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "end")]DateTime? end,
            [FromQuery(Name = "sort_field")]string sortField,
            [FromQuery(Name = "ascending")]bool? ascending,
            [FromQuery(Name = "index")]int index = 1,
            [FromQuery(Name = "size")]int size = 10)
        {
            return this.Ok(this.templateTagService.Query(tagName, tagDescription, start, end ,sortField, ascending, index, size));
        }
      
        /// <summary>
        /// 获取模板标签信息列表(仅当前使用中的)
        /// </summary>
        /// <response code="200">返回模板标记信息列表。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<TemplateTagDto>), 200)]
        public IActionResult GetTempletTagsInUse()
        {
            return this.Ok(this.templateTagService.Get());
        }

        /// <summary>
        /// 获取模板标签信息
        /// </summary>
        /// <param name="id">模板标记信息Id</param>
        /// <response code="200">返回模板标记信息。</response>
        /// <returns></returns>
        [HttpGet("{id}")]
        [ProducesResponseType(typeof(TemplateTagDto), 200)]
        public IActionResult Get([FromRoute(Name = "id")]long id)
        {
            return this.Ok(this.templateTagService.Get(id));
        }
      
        /// <summary>
        /// 获取模板标签信息
        /// </summary>
        /// <param name="tagName">模板标记名称</param>
        /// <response code="200">返回模板标记信息。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(TemplateTagDto), 200)]
        public IActionResult GetTempletTag([FromQuery(Name = "tag_name")]string tagName)
        {
            return this.Ok(this.templateTagService.Get(tagName));
        }
        
        /// <summary>
        /// 添加模板标签信息
        /// </summary>
        /// <param name="templateTagRequestDto">模板标记信息</param>
        /// <response code="201">返回添加成功的模板标记信息。</response>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(TemplateTagDto), 201)]
        public IActionResult Post([FromBody]TemplateTagRequestDto templateTagRequestDto)
        {
            TemplateTagDto result = this.templateTagService.Add(templateTagRequestDto);
            return this.Created(string.Format("api/v1/sim/TemplateTags/{0}", result.TemplateTagId), result);
        }
        
        /// <summary>
        /// 修改模板标签信息
        /// </summary>
        /// <param name="id">模板标记Id</param>
        /// <param name="templateTagRequestDto">模板标记信息</param>
        /// <response code="201">返回修改成功的模板标记信息。</response>
        /// <returns></returns>
        [HttpPut("{id}")]
        [ProducesResponseType(typeof(TemplateTagDto), 201)]
        public IActionResult Put([FromRoute(Name = "id")]long id, [FromBody]TemplateTagRequestDto templateTagRequestDto)
        {
            return this.Ok(this.templateTagService.Update(id, templateTagRequestDto));
        }

        /// <summary>
        /// 删除模板标记信息
        /// </summary>
        /// <param name="id">模板标记信息Id</param>
        /// <response code="204">无内容。</response>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public IActionResult Delete([FromRoute(Name = "id")]long id)
        {
            this.templateTagService.Delete(id);
            return this.NoContent();
        }
    }
}